//
//  PicViewController.m
//  UI_Project_A
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "PicViewController.h"
#import "PunchedLayout.h"
#import "PicCollectionViewCellOfCollection.h"
#import <UIImageView+WebCache.h>


@interface PicViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic ,retain)UIView *viewOfBottom;
@property (nonatomic ,retain)UIButton *buttonOfDisMiss;
@property (nonatomic ,retain)UICollectionView *collection;
@property (nonatomic ,retain)NSMutableArray *arrText;
@property (nonatomic ,retain)NSMutableArray *arrImage;
@property (nonatomic ,retain)UILabel *label;




@end

@implementation PicViewController


- (void)dealloc {

    [_viewOfBottom release];
    [_buttonOfDisMiss release];
    [_collection release];
    [_arrImage release];
    [_arrText release];
    [_label release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];

    [self data];
    [self createCollection];
    [self createView];
    
}





- (void)data {
    
    if (!self.arrText) {
        self.arrText =  [NSMutableArray array];
    }
    if (!self.arrImage) {
        self.arrImage = [NSMutableArray array];
    }
    
    
    NSArray *arrcontect = self.model.content;
    
    for (NSDictionary *dicContect in arrcontect) {
     
        if ([[dicContect objectForKey:@"type"] isEqualToString:@"image"]) {
     
          NSDictionary *data = [dicContect objectForKey:@"data"];
            
            [self.arrText addObject:[data objectForKey:@"text"]];
            
            NSDictionary *dicImage = [data objectForKey:@"big"];
        [self.arrImage addObject:[dicImage objectForKey:@"url"]];
            
        }
    }
}






- (void)createCollection {


    PunchedLayout *flowLayout = [[PunchedLayout alloc] init];
    
    
    flowLayout.itemSize = CGSizeMake(WIDTH - 50, 400);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 15, 100, 10);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 10;
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 50) collectionViewLayout:flowLayout];
    
    [self.view addSubview:self.collection];
    [self.collection release];
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    [self.collection registerClass:[PicCollectionViewCellOfCollection class] forCellWithReuseIdentifier:@"pool"];
    
    
    
    //label
 
    
    self.label = [[UILabel alloc] init];
    [self.view addSubview:self.label];
    
    
    [self.label release];
    self.label.text = @"";
    self.label.numberOfLines = 0;
    
    self.label.textAlignment = 1;
    
    self.label.textColor = [UIColor whiteColor];
    self.label.userInteractionEnabled = YES;
   

    
    
    
   
//手势
    
   UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    
    [self.label addGestureRecognizer:pan];
       [pan release];
}

- (void)panAction:(UIPanGestureRecognizer *)pan{
   
    /** 更改 self.imageView.transform属性 */
    CGPoint point = [pan translationInView:pan.view];
    
    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, 0, point.y);
    /** 参考点归零 */
    [pan setTranslation:CGPointZero inView:pan.view];
    
    
}







-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {



    return self.arrImage.count;
}





- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {



    PicCollectionViewCellOfCollection *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];

  ;
    
   NSString *url = [self Substring:self.arrImage[indexPath.row]];
    
    
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:url]];
    
   

    
    
    
    self.label.frame = CGRectMake(10, HEIGHT - 100, WIDTH - 20, [self heightForLabel:self.arrText[indexPath.row]]);

    
    NSString *str = self.arrText[indexPath.row];
    NSString * labelText = [NSString stringWithFormat:@"%ld/%ld    %@",indexPath.row +1,self.arrText.count,str];
    
    self.label.text = labelText;
    
    return cell;
}


-(CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(WIDTH - 20 , 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    return rect.size.height;
}



#pragma mark - 图片Url截取
- (NSString *)Substring:(NSString *)urlString {
    
    
    NSString *sub = urlString;
    
    if ([sub isEqualToString:@""]) {
        return nil;
    }
    
    
    if ([sub hasSuffix:@"jpg"]) {
        return urlString;
    }
    else {
        
        NSRange range = [urlString rangeOfString:@"u="];
        
        NSString *sub1 = [urlString substringFromIndex:range.location+2];
        
        NSRange lastRange = [sub1 rangeOfString:@"&"];
        sub = [sub1 substringToIndex:lastRange.location];
        
    }
    
    return sub;
}



#pragma mark - 下方功能键
- (void)createView {
    
    self.viewOfBottom = [[UIView alloc] init];
    self.viewOfBottom.frame = CGRectMake(0, HEIGHT - 50, WIDTH, 50);
    [self.view addSubview:self.viewOfBottom];
    [self.viewOfBottom release];
    self.viewOfBottom.backgroundColor = [UIColor blackColor];
    [self createButton];
    
}

#pragma mark - 返回功能
- (void)createButton {
    
    
    UIButton *buttonOfDisMiss = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonOfDisMiss.frame = CGRectMake(10, HEIGHT- 50, 40, 40);
    
    
    [buttonOfDisMiss setImage:[UIImage imageNamed:@"jiantou3"] forState:UIControlStateNormal];
    [self.view addSubview:buttonOfDisMiss];
    
    [buttonOfDisMiss addTarget:self action:@selector(buttonOfDisMissOfPush:) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)buttonOfDisMissOfPush:(UIButton *)button {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
